
package cl.tienda.control;

import cl.tienda.modelo.Producto;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletEConProd extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletEConProd</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletEConProd at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

 
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {        
        PrintWriter out=response.getWriter();
        Vector<Producto> productos=(Vector<Producto>)request.getSession().getAttribute("productos");
        out.println("<html>");
        out.println("<head>");    
        out.println("</head>");
        out.println("<body>");
        
        
        
        out.println("<table align='center'>");
        out.println("<tr>");
        out.print("<td><a href='ServletCConProd.do'>Consultar Productos</a> | </td>");
        out.print("<td><a href=\"ServletEAgrProducto1.view\">Actualizar Stock</a> | </td>");
        out.print("<td><a href=\"login.html\">Salir</a></h4></td>");
        out.println("</tr>");
        out.println("</table>");
        
        //Vamos a mostrar la lista de productos del vector de Productos
        out.println("<table border='2' align='center'>");
        out.println("<tr>");
        out.println("<th>Nombre</th>");
        out.println("<th>Stock</th>");
        out.println("<th>Precio</th>");
        out.println("</tr>");
        for(int i=0; i<productos.size(); i++){
            if(i%2==0){
                out.println("<tr bgcolor=\"#FFFFFF\">");
                out.println("<td>" + productos.get(i).getNombreProducto() + "</td>");
                out.println("<td align=\"right\" valign=\"top\">"+ productos.get(i).getStock() +"</td>");
                out.println("<td align=\"right\" valign=\"top\">"+ productos.get(i).getPrecio() +"</td>");
                out.println("</tr>");
            }else{
                out.println("<tr>");
                out.println("<td>" + productos.get(i).getNombreProducto() + "</td>");
                out.println("<td align=\"right\" valign=\"top\">"+ productos.get(i).getStock() +"</td>");
                out.println("<td align=\"right\" valign=\"top\">"+ productos.get(i).getPrecio() +"</td>");
                out.println("</tr>");
            }
        }

        out.println("</table>");        
        out.println("</table>");
        out.println("</body>");
        out.println("</html>");

        out.close();       
        
        
        
    
    } 
    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

